package toolx

import (
	"gitee.com/chunanyong/zorm"
	"gitee.com/workits/pkgs/dbx"
)

// StructInfo 结构体信息
type StructInfo struct {
	GoModule         string
	ProjModule       string
	ModName          string
	Name             string
	StructName       string
	TableName        string
	FileName         string
	PrimaryKeyColumn string
	PrimaryKeyField  string
	PrimaryKeyType   string
	PrimaryKeyName   string
	Fields           []StructField
	HasTime          bool
	HasDecimal       bool
	Comment          string
	IsStandard       bool
}

// StructField 结构体字段信息
type StructField struct {
	FieldName    string
	ColumnName   string
	JsonName     string
	Type         string
	Required     bool
	Comment      string
	IsPrimaryKey bool
	Default      any
}

// TableInfoEntity 表信息实体
type TableInfoEntity struct {
	zorm.EntityStruct

	Name    string `column:"Name"`
	Comment string `column:"Comment"`

	Fields []*ColumnInfoEntity
}

// ColumnInfoEntity 字段信息实体
type ColumnInfoEntity struct {
	zorm.EntityStruct

	Field   string `column:"Field"`
	Type    string `column:"Type"`
	Null    string `column:"Null"`
	Key     string `column:"Key"`
	Len     int    `column:"len"`
	Default string `column:"Default"`
	Comment string `column:"Comment"`
}

type DBTool interface {
	TableToStructInfo(dbName, tableName, ignorePrefix, structName string, isStandard bool) StructInfo
}

func NewDBTool(dbCfg dbx.Config) DBTool {
	switch dbCfg.Dialect {
	case "mysql":
		return &MySQLTool{dbCfg}
	case "postgresql":
		return &PostgreSQLTool{dbCfg}
	case "sqlite":
		return &SQLiteTool{dbCfg}
	}

	panic("no adapter")
}
